package src.com.neusoft.nep.controller;

import src.com.neusoft.nep.common.AlertUtils;
import src.com.neusoft.nep.common.FileUtils;
import src.com.neusoft.nep.entity.AdminUser;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Map;

public class AdminUserRegisterViewController {
    @FXML
    private Button btn_back;
    @FXML
    private TextField txtPhoneNumber;
    @FXML
    private TextField txtUserName;
    @FXML
    private TextField txtRemark;
    @FXML
    private TextField txtPassword;

    public void saveUser() {
        String phoneNumber = txtPhoneNumber.getText();
        String userName = txtUserName.getText();
        String remark = txtRemark.getText();
        String password = txtPassword.getText();
        AdminUser u1 = new AdminUser(phoneNumber, userName, remark, password);
        Map<String, AdminUser> userMap = FileUtils.readMapObject("adminUser.txt");
        userMap.put(u1.getPhoneNumber(), u1);
        //用户的注册数据文件，通过Map结构存储
        //向用户列表添加新的用户
        FileUtils.writeMapObject("adminUser.txt", userMap);
        AlertUtils.showDialog("用户注册", "注册成功");
    }
    public void back() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../view/AdminUserLoginView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_back.getScene();
        scene.setRoot(root);
        Stage stage = (Stage)scene.getWindow();
        stage.getScene();
        stage.show();
    }
}
